package com.neusoft.neuiotms.dcom.repository.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.QQuestionnaireFeedback;
import com.neusoft.neuiotms.dcom.model.QuestionnaireFeedback;
import com.neusoft.neuiotms.dcom.model.enums.QuestionnaireScope;
import com.neusoft.neuiotms.dcom.model.enums.QuestionnaireType;
import com.neusoft.neuiotms.dcom.repository.CustomizedQuestionnaireFeedbackRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedQuestionnaireFeedbackRepositoryImpl extends AbstractRepository
        implements CustomizedQuestionnaireFeedbackRepository {

    @Override
    public List<QuestionnaireFeedback> index(final Searchable searchable) {
        final QQuestionnaireFeedback qQuestionnaireFeedback = QQuestionnaireFeedback.questionnaireFeedback;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<QuestionnaireFeedback> query = this.query().selectFrom(qQuestionnaireFeedback);
        query.where(where);
        return query.fetch();
    }

    @Override
    protected Class<?> getModelClass() {
        return QuestionnaireFeedback.class;
    }

    @Override
    public Page<QuestionnaireFeedback> search(final Pageable pageable, final Searchable searchable) {
        final QQuestionnaireFeedback qQuestionnaireFeedback = QQuestionnaireFeedback.questionnaireFeedback;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<QuestionnaireFeedback> query = this.query().selectFrom(qQuestionnaireFeedback);
        query.where(where);
        return super.search(query, pageable);
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {

        final BooleanBuilder where = new BooleanBuilder();
        final QQuestionnaireFeedback qQuestionnaireFeedback = QQuestionnaireFeedback.questionnaireFeedback;
        if (!StringUtils.isEmpty(searchable.getStrValue("username"))) {
            where.and(super.equalsStr(qQuestionnaireFeedback.username, searchable, "username"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("user"))) {
            where.and(super.containsStr(qQuestionnaireFeedback.user, searchable, "user"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("submitted"))) {
            where.and(super.equalsBool(qQuestionnaireFeedback.submitted, searchable, "submitted"));
        }

        if (null != searchable.getStrArray("usernameArray")) {
            where.and(super.inStrings(qQuestionnaireFeedback.username, searchable, "usernameArray"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("questionnaireId"))) {
            where.and(super.equalsLong(qQuestionnaireFeedback.questionnaire.id, searchable, "questionnaireId"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("year"))) {
            where.and(super.equalsInt(qQuestionnaireFeedback.questionnaire.year, searchable, "year"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("scope"))) {
            where.and((super.equalsEnum(qQuestionnaireFeedback.questionnaire.scope, searchable, "scope",
                    value -> QuestionnaireScope.valueOf(value))));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("type"))) {
            where.and((super.equalsEnum(qQuestionnaireFeedback.questionnaire.type, searchable, "type",
                    value -> QuestionnaireType.valueOf(value))));
        }

        return where;
    }
}
